package com.wuliupai.delivery.wuliupaidelivery.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wuliupai.delivery.wuliupaidelivery.R;
import com.wuliupai.delivery.wuliupaidelivery.utils.LogUtils;

import java.util.List;


/**
 * Descriptions：蓝牙列表界面Adapter
 * <p>
 * Author：ChenME
 * Date：11/26/2016
 * Email：ibelieve1210@163.com
 */
public class BluetoothListAdapter extends BaseAdapter {

    private List<Object> deviceList;
    private Context context;

    public BluetoothListAdapter(Context context, List<Object> deviceList) {
        this.context = context;
        this.deviceList = deviceList;
    }

    @Override
    public int getCount() {
        return null != deviceList ? deviceList.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return deviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolderPersonalCenter holder;
        if (convertView == null) {
            holder = new ViewHolderPersonalCenter();
            convertView = View.inflate(context, R.layout.item_bluetooth_device_name, null);
            holder.tv_bluetooth = (TextView) convertView.findViewById(R.id.tv_bluetooth);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolderPersonalCenter) convertView.getTag();
        }
        Object object = deviceList.get(position);

        if (object instanceof BluetoothDevice) {
            holder.tv_bluetooth.setText(((BluetoothDevice) object).getName());
            LogUtils.log("蓝牙：" + ((BluetoothDevice) object).getName() + "\t地址：" + ((BluetoothDevice) object).getAddress());
        } else if (object instanceof String) {
            holder.tv_bluetooth.setText((String) object);
        }
        return convertView;
    }

    class ViewHolderPersonalCenter {
        TextView tv_bluetooth;
    }
}